package com.erpanderp.wam.usm.model.service.projectandassignment;

import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.ac.model.entity.project.ProjectAssignment;
import com.erpanderp.wam.usm.model.entity.projectandassignment.EmployeeProject;
import com.erpanderp.wam.usm.model.entity.projectandassignment.Project;
import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.HibernateUtil;

public class EmployeeProjectDAO {
	
	public EmployeeProject assignProjectToEmployee(EmployeeProject employeeProject){
		try{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
		System.out.println("FLUSH MODE: " + session.getFlushMode());
		session.setFlushMode(FlushMode.COMMIT);
		session.save(employeeProject);
		session.getTransaction().commit();
		return employeeProject;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
	
		public List<EmployeeProject> getAllEmployeeProjectDetailsByProjectId(Long projectId,String status) {
			List<EmployeeProject> allEmployeeProjectAssignmentDetails = null;
			try {
				Session session = HibernateUtil.getSessionFactory()
						.getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.COMMIT);
				Query query = session.createQuery("from EmployeeProject ep where ep.project.projectId =:projectId and ep.status =:status");
				query.setCacheable(false).setLong("projectId", projectId).setString("status", status);
				allEmployeeProjectAssignmentDetails = (List<EmployeeProject>) query.list();
				session.getTransaction().commit();

			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
			return allEmployeeProjectAssignmentDetails;
		}

	public List<EmployeeProject> getAllEmployeeProjects() {
		List<EmployeeProject> allEmployeeProjects = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			Query query = session.createQuery("from EmployeeProject ep");
			query.setCacheable(false);
			allEmployeeProjects = (List<EmployeeProject>) query.list();
			session.getTransaction().commit();

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return allEmployeeProjects;
	}
	
	public List<EmployeeProject> getAllEmployeeProjects(Long employeeId) {
		List<EmployeeProject> allEmployeeProjects = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			Query query = session.createQuery("from EmployeeProject ep where ep.employee.employeeId = :employeeId and ep.status = 'ACTIVE'");
			query.setCacheable(false).setLong("employeeId", employeeId);
			allEmployeeProjects = (List<EmployeeProject>) query.list();
			session.getTransaction().commit();

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return allEmployeeProjects;
	}
	
	public List<Project> getAllProjects(Long employeeId) {
		List<Project> projects = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			Query query = session.createQuery("select ep.project from EmployeeProject ep where ep.employee.employeeId = :employeeId");
			query.setCacheable(false).setLong("employeeId", employeeId);
			projects = (List<Project>) query.list();
			session.getTransaction().commit();

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return projects;
	}
	
	public EmployeeProject deleteEmployeeProject(EmployeeProject employeeProject) {
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.getTransaction().begin();
			session.setFlushMode(FlushMode.AUTO);
			session.clear();
			session.saveOrUpdate(employeeProject);
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return employeeProject;
	}

	public static void main(String a[]){
		System.out.println(new EmployeeProjectDAO().getAllProjects(13l) == null);
	}
}
